package com.anrongtec.zcpt.listener;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;

import com.anrongtec.zcpt.R;
import com.anrongtec.zcpt.module.AppModel;
import com.anrongtec.zcpt.util.Util;
import com.anrongtec.zcpt.view.DownloadProgressButton;
import com.art.base.util.ApkUtils;
import com.art.base.util.AppCacheUtils;
import com.art.base.util.FileUtils;
import com.lzy.okhttpserver.download.DownloadInfo;
import com.lzy.okhttpserver.download.DownloadManager;
import com.lzy.okhttpserver.download.DownloadService;
import com.lzy.okhttpserver.listener.DownloadListener;
import com.lzy.okhttputils.request.GetRequest;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.io.File;


/**
 * 带进度条下载按钮的点击监听.    实现按钮和downloadmanager的联动
 * 
 * 主要作用是，判断点击之前的状态，从而来改变按钮的状态。  同时改变下载管理类相应downloadInfo的状态
 * 
 * 需要注意的地方
 * app的url 就是  该app下载任务的appTaskKey--------------------------------------------taskKey
 * 而app的下载地址 是app的URL 还要加上 当前用户登录时候的imei imsi  uuid 三个参数--------URL
 * 
 * @author cxy
 *
 */
public class DownloadProgressButtonClickListener implements OnClickListener{

	private DownloadManager manager = DownloadService.getDownloadManager();
	private DownloadProgressButton button;
	private Context context;
	private AppModel app;
	private DownloadInfo info;
	private ViewHolder holder;
	private AppDownloadListener listener;
	
	public DownloadProgressButtonClickListener(Context c, AppModel a, ViewHolder h){
		context = c;
		app = a;
		holder = h;
		button = holder.getView(R.id.btn_item_app_install);
		
		listener = new AppDownloadListener();
		listener.setUserTag(holder);
		info = manager.getDownloadInfo(app.getUrl());
		if (info != null) {
			//TODO  每次重新拼接下载的url
			info.setUrl(Util.getDownloadUrl(app.getUrl()));
			info.setListener(listener);
			
			refreshButtonState(info, holder);
			
		}else {
			button.setState(DownloadProgressButton.NONE);
		}
		
	}
	
	@Override
	public void onClick(View v) {
		//点击之前按钮的状态
		switch (button.getState()) {
		case DownloadProgressButton.NONE:
		case DownloadProgressButton.ERROR:
			//点击之前无状态，则开始下载
			download(app);
			break;
		case DownloadProgressButton.WAITING:
			
			break;
		case DownloadProgressButton.DOWNLOADING:
			//点击之前是正在下载状态，则点击之后暂停
			manager.pauseTask(app.getUrl());
			break;
		case DownloadProgressButton.PAUSE:
//			dpb_app_detail_download.setState(DownloadProgressButton.DOWNLOADING);
			manager.addTask(info.getTaskKey(), info.getRequest(), info.getListener());
			break;
		case DownloadProgressButton.FINISH:
			//判断下载安装包是否存在
			if (FileUtils.isExist(info.getTargetPath())) {
				//判断该APP是否安装
				if (ApkUtils.isAvailable(context, app.getAppPackName())) {
				}else{
					ApkUtils.install(context, new File(info.getTargetPath()));
				}
			}else {
				//不存在，则重新下载
				manager.removeTask(app.getUrl());
				download(app);
			}
			break;
		case DownloadProgressButton.OPEN:
			//ApkUtils.openAPP(context, app.getAppPackName());
			if (app.getActivity() == null||app.getActivity().equals("")) {//有图标的打开方式
				ApkUtils.openAPP(context,
						app.getAppPackName());
			} else {//无图标打开方式

				ApkUtils.openAPP(context,
						app.getAppPackName(), app.getActivity());
			}


			break;
			
		default:
			break;
		}
	
	
	}
	
	/**
	 * 下载
	 */
	private void download(AppModel app) {
		GetRequest request = new GetRequest(Util.getDownloadUrl(app.getUrl()));
		manager.addTask(app.getAppName() ,app.getUrl(), request, new AppDownloadListener());
		AppCacheUtils.getInstance(context).put(app.getUrl(), app);
	}
	
	
	/**
	 * app下载监听
	 * @author cxy
	 *
	 */
	class AppDownloadListener extends DownloadListener{
		
		@Override
		public void onAdd(DownloadInfo downloadInfo) {
			button.setState(DownloadProgressButton.WAITING);
		}

		@Override
		public void onProgress(DownloadInfo arg0) {
			refreshButtonState(arg0, holder);
		}

		@Override
		public void onFinish(DownloadInfo arg0) {
				//判断该APP是否安装
				if (ApkUtils.isAvailable(context, app.getAppPackName())) {
					button.setState(DownloadProgressButton.OPEN);
				}else{
					button.setState(DownloadProgressButton.FINISH);
				}
		}
		@Override
		public void onError(DownloadInfo arg0, String arg1, Exception arg2) {
			button.setState(DownloadProgressButton.ERROR);
		}
		
	}
	
	
	/**
	 * 更新下载按钮状态
	 * @param holder 
	 */
	private void refreshButtonState(DownloadInfo info, ViewHolder holder) {
		
		DownloadProgressButton progressButton = holder.getView(R.id.btn_item_app_install);
		
		//如果不是空,说明有下载的状态!
		//判断下载状态来修改button的状态
		int state = DownloadProgressButton.NONE;
		switch (info.getState()) {
		case DownloadManager.NONE:
			state = DownloadProgressButton.NONE;
			
			break;
		case DownloadManager.WAITING:
			state = DownloadProgressButton.WAITING;
			
			break;
		case DownloadManager.DOWNLOADING:
			state = DownloadProgressButton.DOWNLOADING;
			progressButton.updateProgress(info.getProgress()*100);
			manager.addTask(app.getUrl(), info.getRequest(), info.getListener());
			break;
		case DownloadManager.PAUSE:
			state = DownloadProgressButton.PAUSE;
			progressButton.updateProgress(info.getProgress()*100);
			
			break;
		case DownloadManager.FINISH:
			state = DownloadProgressButton.FINISH;
			if (ApkUtils.isAvailable(context, app.getAppPackName())) {
				state = DownloadProgressButton.OPEN;
			}
			break;
		case DownloadManager.ERROR:
			state = DownloadProgressButton.ERROR;
			
			break;

		default:
			break;
		}
		progressButton.setState(state);
	}
	
}
